<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRouter } from 'vue-router'
import Toast from './Toast.vue'

const router = useRouter()

// Toast引用
const toastRef = ref(null)
// 登录模态框引用
const loginModalRef = ref(null)
// 当前路由路径
const currentRoute = ref(router.currentRoute.value.path)

// 响应式数据
const isScrolled = ref(false)
const isLoginModalOpen = ref(false)
const isLoginTypePassword = ref(true)
const isFullscreenLoading = ref(false)
const showUserDropdown = ref(false)
const agreementChecked = ref(false)
const searchQuery = ref('')
const loginForm = ref({
  username: '',
  password: '',
  phone: '',
  code: ''
})

// 错误信息
const errors = ref({
  username: '',
  password: '',
  phone: '',
  code: '',
  agreement: ''
})

// 用户信息（临时设置默认登录成功）
const userInfo = ref({
  isLoggedIn: false,
  name: '用户姓名',
  avatar: '/static/images/default-avatar.jpg',
  isVip: false // 是否为VIP会员
})

// 主题模式
const isDarkMode = ref(false)

// 动态Logo文字相关
const logoText = ref('三毛流浪记')
const animatedText = ref('')

// Logo文字动画效果
const animateLogoText = () => {
  const text = logoText.value
  let index = 0
  
  const typeWriter = () => {
    if (index < text.length) {
      animatedText.value += text.charAt(index)
      index++
      setTimeout(typeWriter, 200) // 每200ms显示一个字符
    } else {
      // 动画完成后，等待一段时间再重新开始
      setTimeout(() => {
        animatedText.value = ''
        index = 0
        typeWriter()
      }, 3000) // 3秒后重新开始动画
    }
  }
  
  typeWriter()
}

// 计算属性
const isAgreementShaking = computed(() => !!errors.value.agreement)

// 随机选择头像
const getRandomAvatar = () => {
  const avatarCount = 20; // 头像总数
  const randomIndex = Math.floor(Math.random() * avatarCount) + 1;
  return `/static/images/heads/avatar${randomIndex}.png`;
}

// 方法
const handleScroll = () => {
  isScrolled.value = window.scrollY > 50
}

const openLoginModal = () => {
  isLoginModalOpen.value = true
  resetForm()
  // 添加外部点击关闭事件监听
  setTimeout(() => {
    document.addEventListener('click', closeLoginModalOnClickOutside)
  }, 0)
}

const closeLoginModal = () => {
  isLoginModalOpen.value = false
  resetForm()
  // 移除外部点击关闭事件监听
  document.removeEventListener('click', closeLoginModalOnClickOutside)
}

const closeLoginModalOnClickOutside = (event: MouseEvent) => {
  const modal = loginModalRef.value as HTMLElement | null
  const modalContent = modal?.querySelector('.login-modal-content')
  
  if (modal && modalContent && !modalContent.contains(event.target as Node)) {
    closeLoginModal()
  }
}

const resetForm = () => {
  loginForm.value = {
    username: '',
    password: '',
    phone: '',
    code: ''
  }
  errors.value = {
    username: '',
    password: '',
    phone: '',
    code: '',
    agreement: ''
  }
  agreementChecked.value = false
}

const switchLoginType = (isPassword: boolean) => {
  isLoginTypePassword.value = isPassword
  resetForm()
}

const validateForm = () => {
  let isValid = true
  
  if (isLoginTypePassword.value) {
    if (!loginForm.value.username.trim()) {
      errors.value.username = '请输入用户名'
      isValid = false
    } else {
      errors.value.username = ''
    }
    
    if (!loginForm.value.password.trim()) {
      errors.value.password = '请输入密码'
      isValid = false
    } else {
      errors.value.password = ''
    }
  } else {
    if (!loginForm.value.phone.trim()) {
      errors.value.phone = '请输入手机号'
      isValid = false
    } else {
      errors.value.phone = ''
    }
    
    if (!loginForm.value.code.trim()) {
      errors.value.code = '请输入验证码'
      isValid = false
    } else {
      errors.value.code = ''
    }
  }
  
  if (!agreementChecked.value) {
    errors.value.agreement = '请勾选用户协议'
    isValid = false
  } else {
    errors.value.agreement = ''
  }
  
  return isValid
}

const handleLogin = async () => {
  if (!validateForm()) {
    // 如果是用户协议未勾选，触发动画
    if (!agreementChecked.value) {
      // 动画会自动触发
    }
    return
  }
  
  isFullscreenLoading.value = true
  
  try {
    // 显示登录中提示
    showToast('登录中，请稍后...', 'info')
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1500))
    
    // 临时设置登录成功，随机选择头像
    userInfo.value = {
      isLoggedIn: true,
      name: '张三',
      avatar: getRandomAvatar(),
      isVip: Math.random() < 0.3 // 30%概率成为VIP会员
    }
    
    closeLoginModal()
    
    // 显示成功提示
    showToast('登录成功', 'success')
  } catch (error) {
    showToast('登录失败，请稍后重试', 'error')
  } finally {
    isFullscreenLoading.value = false
  }
}

const showToast = (message: string, type: 'success' | 'error' | 'warning' | 'info') => {
  // 这里应该调用Toast组件的方法
  if (toastRef.value) {
    // @ts-ignore
    toastRef.value.show(message, type)
  }
}

const toggleUserDropdown = () => {
  if (userInfo.value.isLoggedIn) {
    showUserDropdown.value = !showUserDropdown.value
    // 点击其他地方关闭下拉菜单
    if (showUserDropdown.value) {
      setTimeout(() => {
        document.addEventListener('click', closeUserDropdownOnClickOutside)
      }, 0)
    }
  }
}

const closeUserDropdownOnClickOutside = (event: MouseEvent) => {
  const dropdown = document.querySelector('.user-dropdown')
  const userInfo = document.querySelector('.user-info')
  
  if (dropdown && !dropdown.contains(event.target as Node) && 
      userInfo && !userInfo.contains(event.target as Node)) {
    showUserDropdown.value = false
    document.removeEventListener('click', closeUserDropdownOnClickOutside)
  }
}

// 主题切换功能已修改为直接点击切换，无需下拉菜单

const goToProfile = () => {
  showUserDropdown.value = false
  router.push('/main/profile')
}

const logout = () => {
  showUserDropdown.value = false
  userInfo.value = {
    isLoggedIn: false,
    name: '用户姓名',
    avatar: getRandomAvatar(), // 登出后也随机选择头像
    isVip: false // 添加缺失的isVip属性
  }
  showToast('退出登录成功', 'success')
  router.push('/')
}

const isGray = ref(false)

const toggleGray = () => {
  isGray.value = !isGray.value
  if (isGray.value) {
    document.documentElement.style.filter = 'grayscale(1)'
  } else {
    document.documentElement.style.filter = ''
  }
}

// 计算当前激活的导航项
const isActiveNavItem = (path: string) => {
  // 获取当前路由路径，去掉 '/main' 前缀
  const currentPath = currentRoute.value.replace('/main', '') || '/'
  
  // 特殊处理首页
  if (path === '/') {
    return currentPath === '/' || currentPath === '/home' || currentPath === '/index'
  }
  
  // 其他路径直接匹配
  return currentPath === path
}

// 导航项点击处理 - 修改为指向无底部页面
const navigateTo = (path: string) => {
  // 修改导航路径，使用无底部页面
  switch (path) {
    case '/music':
      router.push('/music-no-footer')
      break
    case '/video':
      router.push('/video-no-footer')
      break
    case '/gallery':
      router.push('/gallery-no-footer')
      break
    default:
      // 其他路径保持原有逻辑
      if (path === '/') {
        router.push('/main')
      } else {
        router.push(`/main${path}`)
      }
  }
}

const handleSearch = () => {
  if (searchQuery.value.trim()) {
    // 跳转到搜索结果页面
    router.push({
      path: '/main/search',
      query: { q: searchQuery.value.trim() }
    })
    // 清空搜索框
    searchQuery.value = ''
  }
}

// 添加搜索框聚焦状态
const isSearchFocused = ref(false)

// 搜索框聚焦处理
const handleSearchFocus = () => {
  isSearchFocused.value = true
}

// 搜索框失焦处理
const handleSearchBlur = () => {
  isSearchFocused.value = false
}

const toggleTheme = (mode: 'light' | 'dark') => {
  isDarkMode.value = mode === 'dark'
  if (isDarkMode.value) {
    document.body.classList.add('dark-mode')
    // 显示提示
    showToast('已切换到夜晚模式', 'info')
  } else {
    document.body.classList.remove('dark-mode')
    // 显示提示
    showToast('已切换到白天模式', 'info')
  }
}

// 监听路由变化
watch(() => router.currentRoute.value, (route) => {
  currentRoute.value = route.path
}, { immediate: true })

// 生命周期钩子
onMounted(() => {
  // 启动Logo文字动画
  animateLogoText()
  
  window.addEventListener('scroll', handleScroll)
  // 添加登录模态框外部点击关闭事件监听
  if (isLoginModalOpen.value && loginModalRef.value) {
    setTimeout(() => {
      document.addEventListener('click', closeLoginModalOnClickOutside)
    }, 0)
  }
})

onBeforeUnmount(() => {
  window.removeEventListener('scroll', handleScroll)
  document.removeEventListener('click', closeUserDropdownOnClickOutside)
  document.removeEventListener('click', closeLoginModalOnClickOutside)
})
</script>

<template>
  <header 
    class="app-header" 
    :class="{ 
      scrolled: isScrolled,
      dark: isDarkMode
    }"
  >
    <div class="header-container">
      <div class="logo" @click="router.push('/home')">
        <img src="/static/images/logo.png" alt="网站Logo" class="logo-img" />
        <span class="logo-text">{{ animatedText }}</span>
      </div>

      <!-- 一键置灰按钮 -->
      <button class="gray-toggle-btn" @click="toggleGray">
        {{ isGray ? '恢复正常' : '一键置灰' }}
      </button>
      
      <nav class="nav-menu">
        <ul class="nav-list">
          <li 
            class="nav-item" 
            :class="{ active: isActiveNavItem('/') }"
            @click="navigateTo('/')"
          >
            首页
          </li>
          <li 
            class="nav-item" 
            :class="{ active: isActiveNavItem('/music') }"
            @click="navigateTo('/music')"
          >
            音乐
          </li>
          <li 
            class="nav-item" 
            :class="{ active: isActiveNavItem('/video') }"
            @click="navigateTo('/video')"
          >
            视频
          </li>
          <li 
            class="nav-item" 
            :class="{ active: isActiveNavItem('/gallery') }"
            @click="navigateTo('/gallery')"
          >
            画廊
          </li>
          <li 
            class="nav-item" 
            :class="{ active: isActiveNavItem('/about') }"
            @click="navigateTo('/about')"
          >
            关于
          </li>
        </ul>
      </nav>
      
      <div class="user-section">
        <!-- 搜索框 -->
        <div class="search-container" :class="{ 'search-container--focused': searchQuery }">
          <input 
            type="text" 
            v-model="searchQuery" 
            placeholder="全文搜索..." 
            class="search-input"
            @keyup.enter="handleSearch"
            @focus="handleSearchFocus"
            @blur="handleSearchBlur"
          />
          <button class="search-button" @click="handleSearch">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M15.5 14H14.71L14.43 13.73C15.41 12.59 16 11.11 16 9.5C16 5.91 13.09 3 9.5 3C5.91 3 3 5.91 3 9.5C3 13.09 5.91 16 9.5 16C11.11 16 12.59 15.41 13.73 14.43L14 14.71V15.5L19 20.49L20.49 19L15.5 14ZM9.5 14C7.01 14 5 11.99 5 9.5C5 7.01 7.01 5 9.5 5C11.99 5 14 7.01 14 9.5C14 11.99 11.99 14 9.5 14Z" fill="currentColor"/>
            </svg>
          </button>
        </div>
        
        <!-- 主题切换 -->
        <div 
          class="theme-toggle" 
          :class="{ 'theme-toggle--dark': isDarkMode }"
          @click="toggleTheme(isDarkMode ? 'light' : 'dark')"
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path 
              v-if="!isDarkMode" 
              d="M12 9C10.34 9 9 10.34 9 12C9 13.66 10.34 15 12 15C13.66 15 15 13.66 15 12C15 10.34 13.66 9 12 9ZM12 7C8.43 7 9.24 3.5 9.24 3.5C9.24 3.5 5.74 4.31 5.74 7.88C5.74 7.88 2.24 8.69 2.24 12.26C2.24 12.26 3.05 15.76 6.62 15.76C6.62 15.76 7.43 19.26 11 19.26C11 19.26 14.5 18.45 14.5 14.88C14.5 14.88 18.88 14.07 18.88 10.5C18.88 10.5 15.57 9.69 12 9.69V9Z" 
              fill="currentColor"
            />
            <path 
              v-else
              d="M9.37 5.51C9.19 6.15 9.1 6.82 9.1 7.5C9.1 9.99 10.1 12.24 11.74 13.88C13.38 15.52 15.63 16.52 18.12 16.52C18.8 16.52 19.47 16.43 20.11 16.25C19.25 19.11 16.77 21.25 13.76 21.25C9.62 21.25 6.25 17.88 6.25 13.74C6.25 10.73 8.39 8.25 11.25 7.39Z" 
              fill="currentColor"
            />
          </svg>
        </div>
        
        <!-- 用户信息或登录按钮 -->
        <div v-if="userInfo.isLoggedIn" class="user-info" @click="toggleUserDropdown">
          <div class="user-avatar">
            <img :src="userInfo.avatar" :alt="userInfo.name" />
            <!-- VIP标识 -->
            <span
              v-if="userInfo.isVip"
              class="vip-badge"
              title="VIP会员"
            >VIP</span>
          </div>
          <span class="user-name">{{ userInfo.name }}</span>
          
          <!-- 用户下拉菜单 -->
          <div v-if="showUserDropdown" class="user-dropdown">
            <div class="dropdown-item" @click="goToProfile">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 12C14.21 12 16 10.21 16 8C16 5.79 14.21 4 12 4C9.79 4 8 5.79 8 8C8 10.21 9.79 12 12 12ZM12 14C9.33 14 4 15.34 4 18V20H20V18C20 15.34 14.67 14 12 14Z" fill="currentColor"/>
              </svg>
              个人中心
            </div>
            <div class="dropdown-item" @click="logout">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M17 7L15.59 8.41L18.17 11H8V13H18.17L15.59 15.59L17 17L22 12L17 7ZM4 5H12V3H4C2.9 3 2 3.9 2 5V19C2 20.1 2.9 21 4 21H12V19H4V5Z" fill="currentColor"/>
              </svg>
              退出登录
            </div>
          </div>
        </div>
        
        <div v-else class="login-section">
          <button class="login-btn" @click="openLoginModal">登录</button>
        </div>
      </div>
    </div>
    
    <!-- 全屏登录中遮罩层 -->
    <div v-if="isFullscreenLoading" class="fullscreen-loading">
      <div class="loading-content">
        <div class="spinner"></div>
        <p>登录中，请稍后...</p>
      </div>
    </div>
  </header>
  
  <!-- 登录模态框 -->
  <div v-if="isLoginModalOpen" class="login-modal" ref="loginModalRef">
    <div class="login-modal-content" @click.stop>
      <div class="login-modal-body">
        <div class="login-type-switch">
          <button 
            :class="{ active: isLoginTypePassword }" 
            @click="switchLoginType(true)"
          >
            密码登录
          </button>
          <button 
            :class="{ active: !isLoginTypePassword }" 
            @click="switchLoginType(false)"
          >
            验证码登录
          </button>
        </div>
        
        <div class="login-form-container">
          <div v-if="isLoginTypePassword" class="login-form">
            <div class="form-group">
              <input 
                v-model="loginForm.username" 
                type="text" 
                class="form-input" 
                :class="{ error: errors.username }"
                placeholder="用户名/手机号/邮箱" 
              />
              <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
            </div>
            
            <div class="form-group">
              <input 
                v-model="loginForm.password" 
                type="password" 
                class="form-input" 
                :class="{ error: errors.password }"
                placeholder="密码" 
              />
              <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
            </div>
          </div>
          
          <div v-else class="login-form">
            <div class="form-group">
              <input 
                v-model="loginForm.phone" 
                type="tel" 
                class="form-input" 
                :class="{ error: errors.phone }"
                placeholder="手机号" 
              />
              <div v-if="errors.phone" class="error-message">{{ errors.phone }}</div>
            </div>
            
            <div class="form-group sms-group">
              <input 
                v-model="loginForm.code" 
                type="text" 
                class="form-input sms-input" 
                :class="{ error: errors.code }"
                placeholder="验证码" 
              />
              <button class="sms-button">获取验证码</button>
            </div>
            <div v-if="errors.code" class="error-message">{{ errors.code }}</div>
          </div>
          
          <div class="form-group agreement" :class="{ shake: isAgreementShaking }">
            <label class="checkbox-label">
              <input 
                v-model="agreementChecked" 
                type="checkbox" 
                class="checkbox-input" 
              />
              我已阅读并同意
              <a href="#" class="agreement-link">《用户协议》</a>
              和
              <a href="#" class="agreement-link">《隐私政策》</a>
            </label>
            <div v-if="errors.agreement" class="error-message">{{ errors.agreement }}</div>
          </div>
          
          <button class="submit-btn" @click="handleLogin">登录</button>
          
          <div class="third-party-login">
            <div class="third-party-title">第三方登录</div>
            <div class="third-party-icons">
              <div class="icon-item">
                <div class="icon wechat">微</div>
                <span>微信</span>
              </div>
              <div class="icon-item">
                <div class="icon alipay">支</div>
                <span>支付宝</span>
              </div>
              <div class="icon-item">
                <div class="icon qq">Q</div>
                <span>QQ</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <!-- Toast组件 -->
  <Toast ref="toastRef" />
</template>

<style scoped>
.app-header {
  position: fixed; /* 固定在顶部 */
  top: 0;
  left: 0;
  right: 0; /* 确保导航栏宽度占满整个视口 */
  z-index: 1000; /* 提高z-index确保在最上层 */
  background: rgba(248, 248, 248, 0.95); /* 半透明背景 */
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
  transition: all 0.3s ease;
  transform: translateY(0);
  height: 60px; /* 明确设置高度 */
}

.app-header.scrolled {
  background: rgba(248, 248, 248, 0.98);
  box-shadow: 0 2px 30px rgba(0, 0, 0, 0.15);
}


.app-header.dark {
  background: rgba(30, 30, 30, 0.95);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.3);
}

.app-header.dark.scrolled {
  background: rgba(30, 30, 30, 0.98);
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  height: 60px;
  transition: height 0.3s ease;
}

.app-header.scrolled .header-container {
  height: 50px;
}

.vip-badge {
  position: absolute;
  left: 50%;
  bottom: -8px;
  transform: translateX(-50%);
  background: linear-gradient(90deg, #ffd700 60%, #ff9800 100%);
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  padding: 2px 14px 2px 14px;
  border-radius: 16px 16px 16px 16px / 10px 10px 16px 16px;
  box-shadow: 0 2px 8px rgba(255, 215, 0, 0.18);
  border: 1.5px solid #fff;
  z-index: 2;
  letter-spacing: 2px;
  user-select: none;
  pointer-events: none;
  /* 环绕头像底部的视觉效果 */
}

.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  flex-shrink: 0; /* 防止logo被压缩 */
}

.logo-img {
  height: 40px;
  width: 40px;
  margin-right: 10px;
  object-fit: contain;
  transition: all 0.3s ease;
}

.app-header.scrolled .logo-img {
  height: 30px;
  width: 30px;
}

/* Logo文字动画样式 */
.logo-text {
  font-size: 1.8rem;
  font-weight: 700;
  font-family: 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  color: #646cff;
  position: relative;
  display: inline-block;
  transition: all 0.3s ease;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(100, 108, 255, 0.3);
  /* 确保只影响Logo文字，不影响其他元素 */
  margin-right: 20px;
  flex-shrink: 0;
  /* 固定宽度以防止影响其他元素布局 */
  width: 200px;
  text-align: left;
  overflow: hidden;
  white-space: nowrap;
}

/* 打字机光标效果 */
.logo-text::after {
  content: '|';
  animation: blink 0.8s infinite;
  color: #ff6b6b;
  margin-left: 4px;
  font-weight: normal;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* 滚动时Logo文字样式 */
.app-header.scrolled .logo-text {
  font-size: 1.5rem;
  color: #ff6b6b;
  text-shadow: 0 1px 3px rgba(255, 107, 107, 0.3);
}

/* 深色模式下的Logo文字 */
.app-header.dark .logo-text {
  color: #ffd93d;
  text-shadow: 0 2px 4px rgba(255, 217, 61, 0.3);
}

/* 深色模式滚动时的Logo文字 */
.app-header.dark.scrolled .logo-text {
  color: #646cff;
  text-shadow: 0 1px 3px rgba(100, 108, 255, 0.3);
}

/* 确保页签样式不受影响 */
.nav-menu {
  flex: 1;
  margin: 0 20px;
  min-width: 0; /* 允许导航菜单收缩 */
}

.nav-list {
  display: flex;
  list-style: none;
  justify-content: center;
  margin: 10px 0;
  padding: 0;
}

.nav-item {
  padding: 12px 25px;
  margin: 0 8px;
  cursor: pointer;
  color: #555;
  font-weight: 600;
  font-size: 1rem;
  border-radius: 25px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  writing-mode: horizontal-tb;
  text-orientation: mixed;
  white-space: nowrap;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.5px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  flex-shrink: 0; /* 防止页签被压缩 */
}

.nav-item:hover {
  background: rgba(100, 108, 255, 0.15);
}

.nav-link {
  color: inherit;
  text-decoration: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.app-header.dark .nav-item {
  color: #e0e0e0;
  background: rgba(30, 30, 30, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.nav-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(100, 108, 255, 0.15);
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: -1;
  border-radius: 25px;
}

.nav-item:hover::before {
  opacity: 1;
}

.nav-item:hover {
  color: #646cff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.app-header.dark .nav-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.nav-item.active {
  background: rgba(100, 108, 255, 0.2);
  color: #646cff;
  box-shadow: 0 4px 12px rgba(100, 108, 255, 0.2);
}

.nav-item.fade-out {
  opacity: 0.5;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 15px; /* 增加元素之间的间距 */
  position: relative;
  margin-left: auto; /* 将用户区域推到最右边 */
  margin-right: -230px; /* 右边距 */
  flex-shrink: 0; /* 防止用户区域被压缩 */
}

.search-container {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 20px;
  padding: 2px 2px 2px 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  transition: all 0.3s ease;
  width: 250px;
  flex-shrink: 0;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.search-container:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.search-container--focused {
  box-shadow: 0 4px 15px rgba(100, 108, 255, 0.2);
  border-color: rgba(100, 108, 255, 0.5);
}

.app-header.dark .search-container {
  background: rgba(30, 30, 30, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.app-header.dark .search-container--focused {
  box-shadow: 0 4px 15px rgba(100, 108, 255, 0.3);
  border-color: rgba(100, 108, 255, 0.7);
}

.search-input {
  background: transparent;
  border: none;
  outline: none;
  padding: 8px 10px;
  font-size: 0.9rem;
  color: #333;
  width: 100%;
  transition: all 0.3s ease;
}

.app-header.dark .search-input {
  color: #f8f8f8;
}

.search-input::placeholder {
  color: #999;
}

.app-header.dark .search-input::placeholder {
  color: #aaa;
}

.search-button {
  background: #646cff;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
  padding: 0;
}

.search-button:hover {
  background: #525add;
  transform: scale(1.05);
}

.theme-toggle {
  cursor: pointer;
  color: #333;
  transition: all 0.3s ease;
  padding: 5px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0; /* 防止主题切换按钮被压缩 */
}

.theme-toggle:hover {
  background: rgba(100, 108, 255, 0.2);
  transform: rotate(15deg);
}

.theme-toggle--dark {
  background: rgba(100, 108, 255, 0.2);
  color: #ffd93d;
}

.theme-toggle--dark:hover {
  background: rgba(100, 108, 255, 0.3);
}

.app-header.dark .theme-toggle {
  color: #f8f8f8;
}

.login-section {
  margin-left: 15px;
}

.login-btn {
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.6);
  color: #333;
  padding: 12px 28px;
  border-radius: 25px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 600;
  transition: all 0.3s ease;
  min-width: 100px;
  text-align: center;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  flex-shrink: 0; /* 防止登录按钮被压缩 */
}

.app-header.dark .login-btn {
  background: rgba(30, 30, 30, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #f8f8f8;
}

.login-btn:hover {
  background: rgba(100, 108, 255, 0.2);
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.app-header.dark .login-btn:hover {
  background: rgba(100, 108, 255, 0.3);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
}

.theme-toggle:hover {
  background: rgba(100, 108, 255, 0.1);
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: 2px solid white;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
  position: relative;
  transition: all 0.3s ease;
  flex-shrink: 0; /* 防止头像被压缩 */
}

.user-avatar::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  z-index: -1;
  animation: rotate 3s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.2);
}

.user-name {
  color: #333;
  font-weight: 600;
  font-size: 1rem;
  transition: all 0.3s ease;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  white-space: nowrap; /* 防止用户名换行 */
  flex-shrink: 0; /* 防止用户名被压缩 */
}

.user-info:hover .user-name {
  color: #646cff;
}

.app-header.dark .user-name {
  color: #f8f8f8;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.app-header.dark .user-avatar {
  border-color: #1e1e1e;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.app-header.dark .user-avatar:hover {
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

.app-header.scrolled .user-avatar {
  width: 36px;
  height: 36px;
}

.app-header.scrolled .user-name {
  font-size: 0.9rem;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  gap: 12px;
  padding: 8px 15px;
  border-radius: 30px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  flex-shrink: 0; /* 防止用户信息区域被压缩 */
}

.app-header.dark .user-info {
  background: rgba(30, 30, 30, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.user-info:hover {
  background: rgba(100, 108, 255, 0.15);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.app-header.dark .user-info:hover {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.app-header.scrolled .user-info {
  padding: 6px 12px;
  gap: 10px;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 
    0 10px 25px rgba(0, 0, 0, 0.15),
    0 5px 10px rgba(0, 0, 0, 0.1);
  padding: 8px 0;
  min-width: 140px;
  z-index: 1000;
  margin-top: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  transform-origin: top right;
  animation: dropdownFadeIn 0.2s ease;
}

@keyframes dropdownFadeIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.app-header.dark .user-dropdown {
  background: rgba(30, 30, 30, 0.95);
  box-shadow: 
    0 10px 25px rgba(0, 0, 0, 0.3),
    0 5px 10px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.user-dropdown::before {
  content: '';
  position: absolute;
  top: -10px;
  right: 25px;
  width: 0;
  height: 0;
  border-left: 12px solid transparent;
  border-right: 12px solid transparent;
  border-bottom: 12px solid white;
  filter: drop-shadow(0 -2px 2px rgba(0, 0, 0, 0.1));
}

.app-header.dark .user-dropdown::before {
  border-bottom: 12px solid rgba(30, 30, 30, 0.95);
  filter: drop-shadow(0 -2px 2px rgba(0, 0, 0, 0.2));
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  cursor: pointer;
  color: #333;
  transition: all 0.3s ease;
  font-size: 0.95rem;
  font-weight: 500;
}

.app-header.dark .dropdown-item {
  color: #f8f8f8;
}

.dropdown-item:hover {
  background: linear-gradient(135deg, rgba(100, 108, 255, 0.15) 0%, rgba(100, 108, 255, 0.25) 100%);
  color: #646cff;
  transform: translateX(5px);
}

.app-header.dark .dropdown-item:hover {
  background: linear-gradient(135deg, rgba(100, 108, 255, 0.2) 0%, rgba(100, 108, 255, 0.3) 100%);
}

.dropdown-item svg {
  transition: all 0.3s ease;
}

.dropdown-item:hover svg {
  transform: scale(1.1);
}

/* 登录模态框样式 */
.login-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
  /* 确保模态框在视口中间 */
  margin: 0;
  padding: 0;
}

.login-modal-content {
  background: white;
  border-radius: 15px;
  /* 添加立体效果 */
  box-shadow: 
    0 25px 50px -12px rgba(0, 0, 0, 0.25),
    0 10px 20px -5px rgba(0, 0, 0, 0.15),
    0 5px 10px -3px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  max-height: 90vh;
  overflow-y: auto;
  animation: modalFadeIn 0.3s ease;
  position: relative;
  margin: 0 !important; /* 确保移除任何可能影响居中的 margin */
  align-self: center; /* 确保内容在交叉轴上居中 */
  justify-self: center; /* 确保内容在主轴上居中 */
}

.app-header.dark ~ .login-modal-content {
  background: #1e1e1e;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-modal-body {
  padding: 30px 20px 20px; /* 增加上边距以补偿移除的标题 */
}

.login-type-switch {
  display: flex;
  border: 1px solid #eee;
  border-radius: 20px;
  margin-bottom: 20px;
  overflow: hidden;
  background: #f8f8f8; /* 淡化背景 */
}

.app-header.dark ~ .login-type-switch {
  border: 1px solid #333;
  background: #2d2d2d;
}

.login-type-switch button {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent; /* 透明背景 */
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 0.9rem;
  color: #666; /* 淡化文字颜色 */
}

.app-header.dark ~ .login-type-switch button {
  color: #999;
}

.login-type-switch button.active {
  background: #646cff;
  color: white;
}

.login-form-container {
  position: relative;
  min-height: 150px; /* 确保容器有最小高度 */
}

.form-group {
  margin-bottom: 15px;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
  background: white;
  color: #333;
}

.app-header.dark ~ .form-input {
  border: 1px solid #333;
  background: #2d2d2d;
  color: #f8f8f8;
}

.form-input:focus {
  outline: none;
  border-color: #646cff;
  box-shadow: 0 0 0 2px rgba(100, 108, 255, 0.2);
}

.form-input.error {
  border-color: #f56565;
  box-shadow: 0 0 0 2px rgba(245, 101, 101, 0.2);
}

.sms-group {
  display: flex;
  gap: 10px;
}

.sms-input {
  flex: 1;
}

.sms-button {
  padding: 12px 15px;
  background: #646cff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.sms-button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.sms-button:hover:not(:disabled) {
  background: #525add;
}

.forgot-password, .no-code {
  background: none;
  border: none;
  color: #646cff;
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0;
  text-align: left;
}

.app-header.dark ~ .forgot-password, 
.app-header.dark ~ .no-code {
  color: #646cff;
}

.forgot-password:hover, .no-code:hover {
  text-decoration: underline;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #646cff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background: #525add;
}

.agreement {
  text-align: center;
  font-size: 0.9rem;
  color: #666;
}

.app-header.dark ~ .agreement {
  color: #999;
}

.checkbox-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  flex-wrap: wrap;
}

.checkbox-input {
  width: 16px;
  height: 16px;
  margin: 0;
}

.agreement-link {
  color: #646cff;
  text-decoration: none;
}

.app-header.dark ~ .agreement-link {
  color: #646cff;
}

.agreement-link:hover {
  text-decoration: underline;
}

.third-party-login {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.app-header.dark ~ .third-party-login {
  border-top: 1px solid #333;
}

.third-party-title {
  text-align: center;
  color: #999;
  font-size: 0.9rem;
  margin: 0 0 15px 0;
}

.app-header.dark ~ .third-party-title {
  color: #666;
}

.third-party-icons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 5px;
  background: #f5f5f5;
  transition: all 0.3s ease;
}

.app-header.dark ~ .icon {
  background: #2d2d2d;
}

.icon:hover {
  transform: translateY(-3px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
}

.app-header.dark ~ .icon:hover {
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.icon.wechat {
  background: #2dcb3f;
  color: white;
  font-weight: bold;
  font-size: 1.2rem;
}

.icon.alipay {
  background: #1677ff;
  color: white;
  font-weight: bold;
  font-size: 1.2rem;
}

.icon.qq {
  background: #12b7f5;
  color: white;
  font-weight: bold;
  font-size: 1.2rem;
}

.icon-item span {
  font-size: 0.8rem;
  color: #666;
}

.app-header.dark ~ .icon-item span {
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-container {
    flex-wrap: wrap;
    height: auto;
    padding: 10px 0;
  }
  
  .nav-menu {
    order: 3;
    margin: 10px 0 0;
    width: 100%;
  }
  
  .nav-list {
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  
  .nav-item {
    padding: 8px 20px; /* 在小屏幕上适当减小内边距 */
    margin: 5px 5px; /* 在小屏幕上适当减小外边距 */
    font-size: 0.9rem;
  }
  
  .user-section {
    gap: 10px;
  }
}

@media (max-width: 480px) {
  .login-modal-content {
    margin: 10px;
    max-width: calc(100% - 20px);
  }
  
  .sms-group {
    flex-direction: column;
  }
  
  .third-party-icons {
    gap: 10px;
  }
}

/* 全屏登录中遮罩层样式 */
.fullscreen-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1500;
  backdrop-filter: blur(5px);
}

.loading-content {
  text-align: center;
  color: white;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-content p {
  font-size: 18px;
  margin: 0;
}

/* 添加震动动画样式 */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}

.shake {
  animation: shake 0.5s ease-in-out;
}

/* 错误信息样式 */
.error-message {
  color: #f56565;
  font-size: 0.875rem;
  margin-top: 0.25rem;
  text-align: left;
}

.agreement .error-message {
  text-align: center;
}

.gray-toggle-btn {
  margin-left: 20px;
  padding: 8px 18px;
  border-radius: 20px;
  border: none;
  background: #bbb;
  color: #222;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}
.gray-toggle-btn:hover {
  background: #646cff;
  color: #fff;
}
</style>